package com.sdry.web.controller.template;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.base.Staff;
import com.sdry.entity.template.TemplateExample.Criteria;
import com.sdry.entity.template.Template;
import com.sdry.entity.template.TemplateExample;
import com.sdry.service.template.TemplateServise;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @ClassName: TemplateController
 * @Description: 模板
 * @author tdd
 * @date 2018年8月29日
 * @version V1.0
 */
@Controller
@RequestMapping("/template")
public class TemplateController extends BaseController{

    @Resource
    private TemplateServise templateServise;


    /**
     * 去模板列表页面
     * @return
     */
    @RequestMapping("/pageToTemplate")
    public String pageToTemplate() {

        return "/template/template";
    }

    /**
     * 去模板详情页面
     * @return
     */
    @RequestMapping("/pageToTemplateXq")
    public String pageToTemplateXq(Model model,Long id) {
        if (null != id && id != 0){
            Template template = templateServise.selectByPrimaryKey(id);
            model.addAttribute("template",template);
        }
        return "/template/templateXq";
    }

    /**
     * 添加模板
     * @param response 响应
     * @param template 模板实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertTemplate")
    public void insertTemplate(HttpServletRequest request, HttpServletResponse response, Template template) throws Exception {

        int affact = 0;

        TemplateExample example = new TemplateExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(template.getPreHtml() != null && template.getPreHtml() != "") {
            createCriteria.andPreHtmlEqualTo(template.getPreHtml());
        }
        List<Template> templates = templateServise.selectByExample(example);
        if(templates.size() == 0){
        	//判断session
            HttpSession session  = request.getSession();
            //从session中取出用户身份信息
            Staff staffSession = (Staff) session.getAttribute("staff");
            if(staffSession != null){
            	template.setTremark1(staffSession.getSname());
            }
            //当前时间
            SimpleDateFormat sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd");
            String   date   =   sDateFormat.format(new Date());
            template.setTremark4(date);
            //添加
            affact = templateServise.insertSelective(template);
        }
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询模板信息
     * @param request 请求
     * @param response 响应
     * @param rows 每页记录数
     * @param page 页数
     * @param template 模板信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectTemplateList")
    public void selectTemplateList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "page",required = false)int page,
                                   @RequestParam(value = "rows",required = false)int rows,
                                   Template template) throws Exception {

    	PageHelper.startPage(page, rows);

        TemplateExample example = new TemplateExample();
        Criteria createCriteria =  example.createCriteria();
    	//名称模糊
        if(template.getPreHtml() != null && template.getPreHtml() != "") {
    		createCriteria.andPreHtmlLike("%"+template.getPreHtml()+"%");
    	}
    	//预览提示模糊
        if(template.getTitle() != null && template.getTitle() != "") {
            createCriteria.andTitleLike("%"+template.getTitle()+"%");
        }
		List<Template> list = templateServise.selectByExample(example);
		
		PageInfo<Template> info = new PageInfo<Template>(list);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据条件查询模板信息
     * @param response 响应
     * @param template 条件
     * @throws Exception 异常
     */
    @RequestMapping("/selectTemplateByExample")
    public void selectTemplateByExample(HttpServletResponse response, Template template) throws Exception {
        TemplateExample example = new TemplateExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(template.getPreHtml() != null && template.getPreHtml() != "") {
            createCriteria.andPreHtmlEqualTo(template.getPreHtml());
        }
        //预览提示查询
        if(template.getTitle() != null && template.getTitle() != "") {
            createCriteria.andTitleEqualTo(template.getTitle());
        }
        List<Template> templates = templateServise.selectByExample(example);
        //将对象转化为json
        //JSONObject json = JSONObject.fromObject(templates);
        JSONArray json = JSONArray.fromObject(templates);
        ResponseUtil.write(response, json);
    }
    /**
     * 根据id查询模板信息
     * @param response 响应
     * @param id 模板id
     * @throws Exception 异常
     */
    @RequestMapping("/selectTemplateById")
    public void selectTemplateById(HttpServletResponse response, Long id) throws Exception {

        Template template = templateServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(template);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改模板信息
     * @param response 响应
     * @param template 模板信息
     * @throws Exception 异常
     */
    @RequestMapping("/editTemplate")
    public void editTemplate( HttpServletResponse response,
                              Template template) throws Exception {

        int affct = 0;

        TemplateExample example = new TemplateExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(template.getPreHtml() != null && template.getPreHtml() != "") {
            createCriteria.andPreHtmlEqualTo(template.getPreHtml());
        }
        List<Template> templates = templateServise.selectByExample(example);
        if(templates.size() == 0){
            //修改
            affct = templateServise.updateByPrimaryKeySelective(template);
        }else if(templates.size() == 1 && templates.get(0).getTid().equals(template.getTid())){
            affct = templateServise.updateByPrimaryKeySelective(template);
        }
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除
     * @param response 响应
     * @param ids 模板id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteTemplate")
    public void deleteTemplate( HttpServletResponse response, String ids) throws Exception {

        int affact = templateServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }

}
